2090. K Radius Subarray Averages - LeetCode Solution


two pointer array

Python Code:

from math import floor
class Solution:
    def getAverages(self, nums: List[int], k: int) -> List[int]:
        arr = []
        if len(nums) <= k+k:
            for i in range(len(nums)):
                arr.append(-1)
            return arr
        total_sum = 0
        for i in range(k):
            total_sum+= nums[i]
            arr.append(-1)
        
        for i in range(k, k+k+1,  1):
            total_sum+=nums[i]
            
        
        ptr1 = 0
        ptr2 = k+k
        
        while ptr2 != len(nums)-1:

            arr.append(floor(total_sum/(k+k+1)))
            total_sum-= nums[ptr1]
            ptr1+=1
            ptr2+=1
            total_sum+= nums[ptr2]
        arr.append(floor(total_sum/(k+k+1)))

        for i in range(k):
            arr.append(-1)
        return arr
            
        
            


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses